package com.date.demo02;

import jdk.nashorn.internal.parser.JSONParser;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class Test02 {
    public static void main(String[] args) {
        System.out.println("测试02");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 使用 LocalDateTime 做更加复杂的时间运算 with（）方法
        // 获取本月的第一天 0:00 时刻
        LocalDateTime firstDay = LocalDate.now().withDayOfMonth(1).atStartOfDay();
        System.out.println(dateTimeFormatter.format(firstDay));

        // 获取本月的最后一天
        DateTimeFormatter d2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(d2.format(lastDay));

        // 下个月的第一天
        LocalDate nextMonthFirstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfNextMonth());
        System.out.println(nextMonthFirstDay);

        // 本月第1个周一
        LocalDate firstWeekday = LocalDate.now().with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY));
        System.out.println(firstWeekday);

        // 判断时间先后 LocalDateTime
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime target = LocalDateTime.of(2019,12,10,20,23,20);
        boolean b = now.isBefore(target);
        System.out.println(b); // now 时间在 target 之前吗？ -- false
        boolean b_after = now.isAfter(target);
        System.out.println(b_after); // 在之后 --  true
        // 比较日期
        System.out.println(now.toLocalDate());
        System.out.println(now.toLocalTime());
        boolean d_after = now.toLocalDate().isAfter(LocalDate.of(2020,12,20));
        System.out.println(d_after);
        // 比较时间
        boolean t_after = now.toLocalTime().isAfter(LocalTime.of(12,12,30));
        System.out.println(t_after);
        System.out.println("== 时间间隔 ==");
        // Duration 和 Period 时间间隔
        LocalDateTime start = LocalDateTime.of(2019, 11, 19, 8, 15, 0);
        LocalDateTime end   = LocalDateTime.of(2020,1,9,19,25,30);
        Duration d = Duration.between(start, end);
        System.out.println(d); // PT1235H10M30S 表示 相差 1235小时10分钟30秒
        Period p = start.toLocalDate().until(end.toLocalDate());
        System.out.println(p); // P1M21D 表示 1月21天
    }
}
